package com.shiro.Controller;

import com.shiro.VO.Msg;
import com.shiro.VO.User;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author fangtongle
 * @version 1.0
 * @description
 * @date 2018/4/25
 */
@Controller
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(value = "subLogin", method = RequestMethod.POST)
    @ResponseBody
    public String subLogin(@RequestParam(value = "username", required = true) String username,
                           @RequestParam(value = "password", required = true) String password,
                           @RequestParam(value = "single", required = false) boolean single) {
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        try {
            token.setRememberMe(single);
            subject.login(token);
        } catch (AuthenticationException e) {
            logger.error("-----> 我貌似出错了 <------");
            return e.getMessage();
        }
        try {
            if (subject.hasRole("admin")) {
                return "login success";
            }
        } catch (Exception e) {
            logger.error("------> 授权貌似有点小问题 <--------");
            return e.getMessage();
        }
        return "login failed";
    }

    @RequestMapping(value = "/testRole", method = RequestMethod.GET)
    @ResponseBody
    public String testRole() {
        return "testRole success";
    }

    @RequestMapping(value = "/testRole1", method = RequestMethod.GET)
    @ResponseBody
    public String testRole1() {
        return "testRole1 success";
    }

    @RequestMapping(value = "testPerm", method = RequestMethod.GET)
    @ResponseBody
    public String testPerm() {
        return "testPerm success";
    }

    @RequestMapping(value = "testPerm1", method = RequestMethod.GET)
    @ResponseBody
    public String testPerm1() {
        return "testPerm1 success";
    }

//    @RequestMapping(value = "/testRole", method = RequestMethod.GET)
//    @ResponseBody
//    @RequiresRoles("admin")
//    public String testRole() {
//        return "testRole success";
//    }
//
//    @RequestMapping(value = "/testRole1", method = RequestMethod.GET)
//    @ResponseBody
//    @RequiresRoles("admin1")
//    public String testRole1() {
//        return "testRole1 success";
//    }

}
